package com.example.sid.ui.fragments

import android.text.Editable
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.widget.SwitchCompat
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.Fragment
import com.example.sid.R
import com.example.sid.data.ValueRamStorage
import com.example.sid.ui.fragments.utils.navigateTo

/**
 * Фрагмент для настроек партнёра.
 * Также можно указать всё напрямую в ValueRamStorage
 */
class SetupFragment : Fragment(R.layout.fragment_setup) {
	private val partnerIDText: EditText get() = requireView().findViewById(R.id.partner_id_edit_text)
	private val partnerNameText: EditText get() = requireView().findViewById(R.id.partner_name_edit_text)
	private val partnerProfileUrlText: EditText get() = requireView().findViewById(R.id.partner_profile_url_edit_text)
	private val partnerUrlRedirectText: EditText get() = requireView().findViewById(R.id.partner_redirect_url_edit_text)
	private val partnerScopeText: EditText get() = requireView().findViewById(R.id.partner_scope_edit_text)
	private val partnerStateText: EditText get() = requireView().findViewById(R.id.partner_state_edit_text)
	private val partnerNonceText: EditText get() = requireView().findViewById(R.id.partner_nonce_edit_text)
	private val switchIsShowError: SwitchCompat get() = requireView().findViewById(R.id.is_show_error)
	private val toCaseList: Button get() = requireView().findViewById(R.id.to_case_list)

	override fun onStart() {
		super.onStart()
		resetSetupFromStorage()
		setListeners()
	}

	private fun setListeners() {
		toCaseList.setOnClickListener {
			navigateTo(SetupFragmentDirections.actionSetupFragmentToRootFragment())
		}
		//Ну или есть вариант заполнить эти поля отсюда.
		partnerIDText.doAfterTextChanged { text: Editable? ->
			ValueRamStorage.partnerID = text.toString()
		}
		partnerNameText.doAfterTextChanged {  text: Editable? ->
			ValueRamStorage.partnerName = text.toString()
		}
		partnerProfileUrlText.doAfterTextChanged {  text: Editable? ->
			ValueRamStorage.partnerProfileURL = text.toString()
		}
		partnerUrlRedirectText.doAfterTextChanged {  text: Editable? ->
			ValueRamStorage.partnerUrlRedirect = text.toString()
		}
		partnerScopeText.doAfterTextChanged {  text: Editable? ->
			ValueRamStorage.partnerScope = text.toString()
		}
		partnerStateText.doAfterTextChanged {  text: Editable? ->
			ValueRamStorage.partnerState = text.toString()
		}
		partnerNonceText.doAfterTextChanged {  text: Editable? ->
			ValueRamStorage.partnerNonce = text.toString()
		}
		switchIsShowError.setOnCheckedChangeListener { buttonView, isChecked ->
			ValueRamStorage.isShowError = isChecked
		}
	}

	private fun resetSetupFromStorage() {
		val id = ValueRamStorage.partnerID
		val name = ValueRamStorage.partnerName
		val url = ValueRamStorage.partnerProfileURL
		val urlRedirect = ValueRamStorage.partnerUrlRedirect
		val scope = ValueRamStorage.partnerScope
		val state = ValueRamStorage.partnerState
		val nonce = ValueRamStorage.partnerNonce
		val isError = ValueRamStorage.isShowError
		partnerIDText.setText(id)
		partnerNameText.setText(name)
		partnerProfileUrlText.setText(url)
		partnerScopeText.setText(scope)
		partnerStateText.setText(state)
		partnerNonceText.setText(nonce)
		partnerUrlRedirectText.setText(urlRedirect)
		switchIsShowError.isChecked = isError
	}
}